/**
 * 
 */
package com.google.code.onlineshopping4j.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.onlineshopping4j.dto.ProductInfo;
import com.google.code.onlineshopping4j.exception.ErrorCode;
import com.google.code.onlineshopping4j.exception.OnlineShoppingException;
import com.google.code.onlineshopping4j.exception.ProductNotFoundExcpetion;
import com.google.code.onlineshopping4j.service.DashBoardService;
import com.google.code.onlineshopping4j.dao.ApplicationUserDAO;
import com.google.code.onlineshopping4j.dao.ProductDAO;
import com.google.code.onlineshopping4j.model.Product;

/**
 * @author TelmilaJ
 *
 */
@Service
public class DashBoardServiceImpl implements DashBoardService {

	@Autowired
	private ApplicationUserDAO applicationUserDAO;
	
	@Autowired
	private ProductDAO productDAO;
	
	@Override
	public ProductInfo getProduct(Long productId) throws ProductNotFoundExcpetion {
		Product product = productDAO.findOne(productId);
		return ProductInfo.getAsProduct(product);
	}

	@Override
	public List<ProductInfo> getAllProduct() throws OnlineShoppingException {
		List<ProductInfo> productInfoList = new ArrayList<ProductInfo>();
		try {
			List<Product> products = productDAO.findAll();
			if (products != null && !products.isEmpty()) {
				for (Product product : products) {
					ProductInfo productInfo = ProductInfo.getAsProduct(product);
					productInfoList.add(productInfo);
				}
			}
		} catch (Exception exp) {
			throw new OnlineShoppingException(ErrorCode.SERVER_ERROR, exp.getCause());
		}
		return productInfoList;
	}
}
